package com.central.user.controller;

import java.util.List;
import java.util.Map;

import com.central.common.annotation.RequiredPermission;
import com.central.common.constant.PermissionConstants;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.common.model.SysRole;
import com.central.user.service.ISysRoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author 作者 owen E-mail: 624191343@qq.com
 * 角色管理
 */
@Slf4j
@RestController
@Api(tags = "角色模块api")
public class SysRoleController {
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 后台管理查询角色
     * @param params
     * @return
     */
    @ApiOperation(value = "后台管理查询角色")
    @GetMapping("/roles")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    public PageResult<SysRole> findRoles(@RequestParam Map<String, Object> params) {
        return sysRoleService.findRoles(params);
    }


    @ApiOperation(value = "查询所有角色")
    @GetMapping("/roles/{type}")
    public List<SysRole> findAll(@PathVariable Integer type) {
        List<SysRole> roles = sysRoleService.findAll(type);
        return roles;
    }

    /**
     * 角色新增或者更新
     *
     * @param sysRole
     * @return
     */
    @ApiOperation(value = "角色新增或者更新")
    @PostMapping("/roles/saveOrUpdate")
    @CacheEvict(value ="roleMap",allEntries=true)
    @RequiredPermission({PermissionConstants.SYS_ADMIN})
    public Result saveOrUpdate(@RequestBody SysRole sysRole) throws Exception {
        return sysRoleService.saveOrUpdateRole(sysRole);
    }

    /**
     * 后台管理删除角色
     * delete /role/1
     *
     * @param id
     */
    @ApiOperation(value = "后台管理删除角色")
    @DeleteMapping("/roles/{id}")
    @CacheEvict(value = "roleMap",allEntries=true)
    @RequiredPermission({PermissionConstants.SYS_ADMIN})
    public Result deleteRole(@PathVariable Long id) {
        try {
            sysRoleService.deleteRole(id);
            return Result.succeed("操作成功");
        } catch (Exception e) {
            log.error("role-deleteRole-error", e);
            return Result.failed("操作失败");
        }
    }

    @ApiOperation(value = "查询角色字典 roleId->role")
    @Cacheable(value = "roleMap")
    @GetMapping("/roles/findRoleMap")
    public Map<String,SysRole> findRoleMap(){
        return sysRoleService.findRoleMap();
    }
}
